﻿Imports System
Imports System.Web
Imports System.Xml
Imports System.Text
Imports System.Data
Imports System.Data.OleDb

Public Class RSSFeed : Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        'send document as response
        context.Response.ContentType = "text/xml"
        context.Response.ContentEncoding = System.Text.Encoding.UTF8
        context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(3600))
        context.Response.Cache.SetCacheability(HttpCacheability.Public)

        'create RSS xml document
        Dim xml As New XmlTextWriter(context.Response.OutputStream, Encoding.UTF8)
        xml.Formatting = Formatting.Indented

        xml.WriteStartDocument()
        xml.WriteStartElement("rss")
        xml.WriteAttributeString("version", "2.0")

        'create feed header section
        xml.WriteStartElement("channel")
        xml.WriteElementString("title", "BookMeet - Latest Books Added!")
        xml.WriteElementString("link", "http://www.bookmeet.co.uk")
        xml.WriteElementString("description", "The latest books that our members have added!")
        xml.WriteElementString("language", "en-gb")
        xml.WriteElementString("pubDate", DateTime.UtcNow.ToString("r"))
        xml.WriteElementString("lastBuildDate", DateTime.UtcNow.ToString("r"))
        xml.WriteElementString("ttl", "60")

        'get latest books
        Dim ds As DataSet = GetArticles()

        'add stories to feed
        Dim row As DataRow
        For Each row In ds.Tables("RSSContent").Rows
            xml.WriteStartElement("item")
            xml.WriteElementString("title", row("Title"))
            xml.WriteElementString("author", row("Author"))


            'output story as cdata
            xml.WriteStartElement("description")
            xml.WriteCData(row("Blurb"))
            xml.WriteEndElement()
            xml.WriteEndElement()

        Next

        'close channel
        xml.WriteEndElement()
        'close rss
        xml.WriteEndElement()
        'close document
        xml.WriteEndDocument()
        xml.Flush()
        xml.Close()

    End Sub

    Private Function GetArticles() As DataSet

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)

        Dim SqlString As String = "SELECT TOP 10 ID, Title, Author, Blurb FROM booklist ORDER BY ID DESC"

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)

        cmd.CommandType = CommandType.Text

        Dim ds As New DataSet

        oleDbConn.Open()
        Dim da As New OleDbDataAdapter(cmd)
        da.Fill(ds, "RSSContent")
        Dim maxRows As Integer
        maxRows = ds.Tables("RSSContent").Rows.Count
        oleDbConn.Close()
        cmd.Dispose()
        oleDbConn.Dispose()

        Return ds
    End Function

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class
